<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">

				<el-form-item>
					<el-input v-model="filters.schoolstatus" placeholder="学校名称"></el-input>
				</el-form-item>
				<el-form-item label="学校状态"  >
					<el-select v-model="filters.schoolstatus" placeholder="请选择学校状态" >
						<el-option value="0" label="全部">全部</el-option>
						<el-option value="1" label="全部">未使用</el-option>
						<el-option value="2" label="全部">已使用</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getSchool">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary"  @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<template>
			<el-table :data="schools" highlight-current-row v-loading="loading" border max-height="620" style="width: 100%;">
				<el-table-column type="index" width="80" label="编号">
				</el-table-column>
				<el-table-column prop="schoolname" label="学校名称" >
				</el-table-column>
				<el-table-column prop="schoolstatus" label="状态" :formatter="formatSchoolStatus" >
				</el-table-column>
				<el-table-column prop="createtime" label="创建时间"   >
				</el-table-column>

				<el-table-column label="操作"    width="220">
					<template scope="scope"   >
						<el-button size="small"   @click="handleEnable(scope.$index, scope.row)">
						 编辑
						</el-button>
					</template>
				</el-table-column>
			</el-table>
		</template>
		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>
		<!--新增界面-->
		<el-dialog title="新增学校" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="学校名称" prop="username">
					<el-input v-model="addForm.schoolname" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年级"  >
					<el-select v-model="addForm.gradeid" @change="changeGrade"  multiple placeholder="请选择年级">
						<el-option
								v-for="item in grades"
								:key="item.gradeid"
								:label="item.gradename"
								:value="item.gradeid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="班级名称" >
					<el-input  type="textarea" v-model="addForm.classname" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
				<el-button @click.native="addFormVisible = false">取消</el-button>
			</div>
		</el-dialog>
	</section>

</template>
<script>
	import { findPageSchool,findAllGrade} from '../../api/api';
	export default {
		data() {
			return {
				filters: {
                    schoolstatus: '',
                    schoolname:''
				},
				start:0,
				pageSize:10,
                total:0,
				loading: false,
                addLoading: false,
                addFormVisible : false,
				schools: [],
                grades:[],
                addForm:{
                    gradeid:'',
                    schoolname:'',
                    classname:''
				},
                addFormRules:{}
			}
		},
		methods: {
            formatSchoolStatus: function (row, column) {
                return row.schoolstatus === 1 ? '未使用' : row.schoolstatus === 2 ? '已使用' : "其他";
            },
			getSchool: function () {
				let para = {
                    schoolname: this.filters.schoolname,
					start:this.start,
					pageSize:this.pageSize,
                    schoolstatus:this.filters.schoolstatus
				};
				this.loading = true;
                findPageSchool(para).then(datas => {
                    this.loading = false;
                    //NProgress.done();
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.schools = data.result;
                        this.total = data.totalCount;
                    }
                }).catch(e =>{

				});
			},
            findAllGrade: function () {
                findAllGrade().then(datas => {
                    this.loading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.grades = data;
                    }
                }).catch(e =>{

                });
            },
			handleCurrentChange:function(val){
                this.start = val - 1;
                this.getSchool();
			},
            handleSizeChange:function(val){
			    this.pageSize = val;
			    this.getSchool();
			},
            handleEnable: function (index, row) {

                this.$confirm('确认更新该商家状态吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = {
//                        userstatus: row.userstatus == "2" ? "1" : "2",
//                        useruuid: row.useruuid
                    };
                    handleEnableShop(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        this.getSchool();
                    });
                }).catch(() => {

                });
            }, handleAdd: function () {
                this.findAllGrade();
                this.addFormVisible = true;
                this.addForm = {gradeid:''};
            },changeGrade:function(){
                this.addForm.classname = this.addForm.gradeid;
			}
		},
		mounted() {
			this.getSchool();
		},
    	beforeCreate: function (){
//            window.location.reload();
		}
	};

</script>

<style scoped>

</style>
